using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace VirtualManagerSA
{
	/// <summary>
	/// This is the class that runs this application. It creates one new instance of the Session object,
	/// which represents the backend's main runtime. This class is the root of the GUI class tree, as it spawns
	/// and claims ownership to every other window that is created. It passes the one Session from class to class
	/// as an argument to their constructors every time it, or one of its children, creates a new Form. Once this 
	/// window has been exited, the application closes.
	/// </summary>
	public class LoginWindow : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button loginButton;
		private System.Windows.Forms.Label userLabel;
		private System.Windows.Forms.Label passwordLabel;
		private System.Windows.Forms.TextBox userField;
		private System.Windows.Forms.TextBox passwordField;
		private System.Windows.Forms.RadioButton employeeButton;
		private System.Windows.Forms.RadioButton managerButton;
		private System.Windows.Forms.GroupBox loginGroup;
		private System.Windows.Forms.Button exitButton;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		Session ses;


		public LoginWindow()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			ses = new Session();
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.userLabel = new System.Windows.Forms.Label();
			this.passwordLabel = new System.Windows.Forms.Label();
			this.userField = new System.Windows.Forms.TextBox();
			this.passwordField = new System.Windows.Forms.TextBox();
			this.loginGroup = new System.Windows.Forms.GroupBox();
			this.employeeButton = new System.Windows.Forms.RadioButton();
			this.loginButton = new System.Windows.Forms.Button();
			this.managerButton = new System.Windows.Forms.RadioButton();
			this.exitButton = new System.Windows.Forms.Button();
			this.loginGroup.SuspendLayout();
			this.SuspendLayout();
			// 
			// userLabel
			// 
			this.userLabel.Location = new System.Drawing.Point(32, 40);
			this.userLabel.Name = "userLabel";
			this.userLabel.Size = new System.Drawing.Size(64, 16);
			this.userLabel.TabIndex = 1;
			this.userLabel.Text = "Username: ";
			this.userLabel.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
			// 
			// passwordLabel
			// 
			this.passwordLabel.Location = new System.Drawing.Point(32, 80);
			this.passwordLabel.Name = "passwordLabel";
			this.passwordLabel.Size = new System.Drawing.Size(64, 16);
			this.passwordLabel.TabIndex = 1;
			this.passwordLabel.Text = "Password:";
			this.passwordLabel.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
			// 
			// userField
			// 
			this.userField.Location = new System.Drawing.Point(96, 40);
			this.userField.Name = "userField";
			this.userField.Size = new System.Drawing.Size(96, 20);
			this.userField.TabIndex = 1;
			this.userField.Text = "";
			// 
			// passwordField
			// 
			this.passwordField.Location = new System.Drawing.Point(96, 80);
			this.passwordField.Name = "passwordField";
			this.passwordField.PasswordChar = '●';
			this.passwordField.Size = new System.Drawing.Size(96, 20);
			this.passwordField.TabIndex = 2;
			this.passwordField.Text = "";
			// 
			// loginGroup
			// 
			this.loginGroup.Controls.Add(this.employeeButton);
			this.loginGroup.Controls.Add(this.loginButton);
			this.loginGroup.Controls.Add(this.userLabel);
			this.loginGroup.Controls.Add(this.passwordLabel);
			this.loginGroup.Controls.Add(this.passwordField);
			this.loginGroup.Controls.Add(this.userField);
			this.loginGroup.Controls.Add(this.managerButton);
			this.loginGroup.Location = new System.Drawing.Point(8, 8);
			this.loginGroup.Name = "loginGroup";
			this.loginGroup.Size = new System.Drawing.Size(232, 192);
			this.loginGroup.TabIndex = 0;
			this.loginGroup.TabStop = false;
			this.loginGroup.Text = "Welcome";
			// 
			// employeeButton
			// 
			this.employeeButton.Location = new System.Drawing.Point(40, 120);
			this.employeeButton.Name = "employeeButton";
			this.employeeButton.Size = new System.Drawing.Size(80, 16);
			this.employeeButton.TabIndex = 3;
			this.employeeButton.Text = "Employee";
			// 
			// loginButton
			// 
			this.loginButton.Location = new System.Drawing.Point(64, 152);
			this.loginButton.Name = "loginButton";
			this.loginButton.Size = new System.Drawing.Size(104, 24);
			this.loginButton.TabIndex = 5;
			this.loginButton.Text = "Login";
			this.loginButton.Click += new System.EventHandler(this.loginButton_Click);
			// 
			// managerButton
			// 
			this.managerButton.Location = new System.Drawing.Point(128, 120);
			this.managerButton.Name = "managerButton";
			this.managerButton.Size = new System.Drawing.Size(80, 16);
			this.managerButton.TabIndex = 4;
			this.managerButton.Text = "Manager";
			// 
			// exitButton
			// 
			this.exitButton.Location = new System.Drawing.Point(144, 216);
			this.exitButton.Name = "exitButton";
			this.exitButton.Size = new System.Drawing.Size(96, 24);
			this.exitButton.TabIndex = 7;
			this.exitButton.Text = "Exit";
			this.exitButton.Click += new System.EventHandler(this.exitButton_Click);
			// 
			// LoginWindow
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(250, 248);
			this.Controls.Add(this.exitButton);
			this.Controls.Add(this.loginGroup);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.Name = "LoginWindow";
			this.Text = "Virtual Scheduler SA - Login Window";
			this.loginGroup.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new LoginWindow());
		}

		private void loginButton_Click(object sender, System.EventArgs e)
		{
			if (ses.checkAdmin(userField.Text,passwordField.Text)) 
			{
				userField.Text = "";
				passwordField.Text = "";
				employeeButton.Checked = false;
				managerButton.Checked = false;
				Form f = new AdminWelcome(ses);
				this.AddOwnedForm(f);
				f.Owner = this;
				f.Visible = true;
				f.Activate();
				this.Visible = false;
				return;
			}
			if (!managerButton.Checked && !employeeButton.Checked) 
			{
				MessageBox.Show("Please specify whether this is a manager or employee login.");
				return;
			}
			if (ses.login(userField.Text, passwordField.Text, managerButton.Checked)) 
			{
				if (managerButton.Checked == true) 
				{
					userField.Text = "";
					passwordField.Text = "";
					employeeButton.Checked = false;
					managerButton.Checked = false;
					Form f = new ManagerWelcome(ses);
					this.AddOwnedForm(f);
					f.Owner = this;
					f.Visible = true;
					f.Activate();
					this.Visible = false;
				} 
				else
				{
					userField.Text = "";
					passwordField.Text = "";
					employeeButton.Checked = false;
					managerButton.Checked = false;
					Form f = new EmployeeWelcome(ses);
					this.AddOwnedForm(f);
					f.Owner = this;
					f.Visible = true;
					f.Activate();
					this.Visible = false;
				} 
			} 
			else 
			{
				return;
			}
		}

		private void exitButton_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}


	}
}
